package com.volcreate.week2_3.no7_8_9;

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int year = 0;
        int month = 0;
        int day = 0;

        int choice = input.nextInt();

        if (choice == 1) { // test getNextNDays method
            int m = 0;
            year = Integer.parseInt(input.next());
            month = Integer.parseInt(input.next());
            day = Integer.parseInt(input.next());

            DateUtil date = new DateUtil(year, month, day);

            if (!date.checkInputValidity()) {
                System.out.println("Wrong Format");
                System.exit(0);
            }

            m = input.nextInt();

            if (m < 0) {
                System.out.println("Wrong Format");
                System.exit(0);
            }

            System.out.print(date.getYear() + "-" + date.getMonth() + "-" + date.getDay() + " next " + m + " days is:");
            System.out.println(date.getNextNDays(m).showDate());
        } else if (choice == 2) { // test getPreviousNDays method
            int n = 0;
            year = Integer.parseInt(input.next());
            month = Integer.parseInt(input.next());
            day = Integer.parseInt(input.next());

            DateUtil date = new DateUtil(year, month, day);

            if (!date.checkInputValidity()) {
                System.out.println("Wrong Format");
                System.exit(0);
            }

            n = input.nextInt();

            if (n < 0) {
                System.out.println("Wrong Format");
                System.exit(0);
            }

            System.out.print(
                    date.getYear() + "-" + date.getMonth() + "-" + date.getDay() + " previous " + n + " days is:");
            System.out.println(date.getPreviousNDays(n).showDate());
        } else if (choice == 3) {    //test getDaysofDates method
            year = Integer.parseInt(input.next());
            month = Integer.parseInt(input.next());
            day = Integer.parseInt(input.next());

            int anotherYear = Integer.parseInt(input.next());
            int anotherMonth = Integer.parseInt(input.next());
            int anotherDay = Integer.parseInt(input.next());

            DateUtil fromDate = new DateUtil(year, month, day);
            DateUtil toDate = new DateUtil(anotherYear, anotherMonth, anotherDay);

            if (fromDate.checkInputValidity() && toDate.checkInputValidity()) {
                System.out.println("The days between " + fromDate.showDate() +
                        " and " + toDate.showDate() + " are:"
                        + fromDate.getDaysofDates(toDate));
            } else {
                System.out.println("Wrong Format");
                System.exit(0);
            }
        } else {
            System.out.println("Wrong Format");
            System.exit(0);
        }
    }
}

/**
 * @author zxc
 * @date 2020/9/21
 */
class DateUtil {

    private int year;
    private int month;
    private int day;

    private static final int[] mon = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
    private static final int[] mon2 = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

    public DateUtil(int year, int month, int day) {
        this.year = year;
        this.month = month;
        this.day = day;
    }

    public DateUtil() {
    }

    /**
     * year∈[1820,2020] ,month∈[1,12] ,day∈[1,31]
     * 检测输入的年、月、日是否合法
     *
     * @return
     */
    public boolean checkInputValidity() {
        if (year >= 1820 && year <= 2020) {
            if (month >= 1 && month <= 12) {
                return day >= 1 && day <= 31;
            }
        }
        return false;
    }

    /**
     * 判断year是否为闰年
     *
     * @param year
     * @return
     */
    public boolean isLeapYear(int year) {
        return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
    }


    /**
     * 取得year-month-day的下n天日期
     * 1 1999 3 28 6543
     * 1999-3-28 next 6543 days is:2017-2-24
     *
     * @param n
     * @return
     */
    public DateUtil getNextNDays(int n) {
        int year = this.year;
        int month = this.month;
        int day = this.day;
        for (int i = 0; i < n; i++) {
            day++;
            if (day > mon2[month - 1] + (month == 2 && isLeapYear(year) ? 1 : 0)) {
                day = 1;
                month++;
                if (month > 12) {
                    month = 1;
                    year++;
                }
            }
        }
        return new DateUtil(year, month, day);
    }


    /**
     * 取得year-month-day的前n天日期
     * 2 1834 2 17 7821
     * 1834-2-17 previous 7821 days is:1812-9-19
     *
     * @param n
     * @return
     */
    public DateUtil getPreviousNDays(int n) {
        int year = this.year;
        int month = this.month;
        int day = this.day;
        for (int i = 0; i < n; i++) {
            day--;
            if (day <= 0) {
                month--;
                if (month <= 0) {
                    month = 12;
                    year--;
                }
                day = (mon2[month - 1] + (month == 2 && isLeapYear(year) ? 1 : 0));
            }
        }
        return new DateUtil(year, month, day);
    }


    public boolean compareDates(DateUtil date) {
        if (this.year > date.year) return true;


        if (this.year == date.year) {
            if (this.month > date.month) return true;


            if (this.month == date.month) {
                if (this.day >= date.day) return true;
            }
        }

        return false;
    }


    /**
     * 判断两个日期是否相等
     *
     * @param date
     * @return
     */
    public boolean equalTwoDates(DateUtil date) {
        if (date != null) {
            if (year == date.year && month == date.month && day == date.day) {
                return true;
            }
        }
        return false;
    }


    /**
     * 求当前日期与date之间相差的天数
     * 3 2014 2 14 2020 6 14 => 2312
     * 3 2014 2 13 2020 6 17 => 2316
     * <p>
     * 3 2000 9 22 2012 12 14  => 4466
     *
     * @param date
     * @return
     */
    public int getDaysofDates(DateUtil date) {
        DateUtil dateUtil1 = this; // 小
        DateUtil dateUtil2 = date; // 大
        if (this.compareDates(date)) {
            dateUtil1 = date;
            dateUtil2 = this;
        }

        int days;
        int leapYearNum = 0;
        for (int i = dateUtil1.getYear(); i < dateUtil2.getYear(); i++) {
            if (isLeapYear(i)) {
                leapYearNum++;
            }
        }

        days = 365 * (dateUtil2.getYear() - dateUtil1.getYear()) + leapYearNum;

        int d1 = mon[dateUtil1.getMonth() - 1] + dateUtil1.getDay() + (dateUtil1.getMonth() > 2 && isLeapYear(dateUtil1.getYear()) ? 1 : 0);
        int d2 = mon[dateUtil2.getMonth() - 1] + dateUtil2.getDay() + (dateUtil2.getMonth() > 2 && isLeapYear(dateUtil2.getYear()) ? 1 : 0);
        return days - d1 + d2;
    }


    /**
     * 以“year-month-day”格式返回日期值
     *
     * @return
     */
    public String showDate() {
        return year + "-" + month + "-" + day;
    }


    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public int getMonth() {
        return month;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public int getDay() {
        return day;
    }

    public void setDay(int day) {
        this.day = day;
    }


}
